home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
boe.pres.k12.wv.us
/
boe.pres.k12.wv.us.zip
/
boe.pres.k12.wv.us
/
Utilities
/
Xerox Workcentre 5335
/
Windows Scan
/
64-bit_x64
/
Japanese
/
cpsimage.cab
/
data
/
sys
/
zip.elf
< prev
Wrap
Text File
|
2009-04-23
|
10KB
|
256 lines
/*
** $Id: zip.elf,v 1.8 2009/04/22 14:42:51 campanel Exp $
*/
#load "sys/stdio.elf";
/******************************************************************************/
/*
** This class provides access to creating zip files.
*/
/* @New
// DESCRIPTION
Creates a new instance of ZIP for creating a zip archive.
// ARGUMENTS
FILE file The output zip file.
STRING encoding The encoding of the file names. The default is UTF-8.
BOOLEAN append Files are added to an existing archive, otherwise the output is overwritten.
// EXAMPLE's
#load "sys/zip.elf";
//-------------------------------------------------------
FILE zipfile = new (FILE, path: "output.zip");
ZIP zip = new (ZIP, file:zipfile);
FILE f; // This is the same as FILE f = new ();
LIST files = f.list(); // List the contents of the current directory
zip.addFiles( dir:f, list:files ); // Add all files from the current dir
zip.close();
//-------------------------------------------------------
zipfile = new (FILE, path: "other.zip");
zip = new (ZIP, file:zipfile);
f = new (FILE, path:"datadir");
zip.addFiles( dir:f, recurse:TRUE );
zip.close();
//-------------------------------------------------------
zipfile = new (FILE, path:"other.zip");
zip = new (ZIP, file:zipfile);
zip.setOption( option:ZIP.NODIRS ); // Don't store directory references
FILE f = new (FILE, path:"doc"); // Store all of files in the doc dir
zip.addFiles( dir:f, recurse:TRUE ); // recurse down also
zip.close();
*/
/* @addFile Adds a single file to the archive. The file is stored relative to
the given directory, therefore the file should exists below the given
directory. If no directory is specified then it is placed in the top level,
i.e., "." */
/* @addFiles Adds a directory of files to the archive. If a list is given then
those files are added. Otherwise all files in the directory are stored. If
recurse is set to true then all directories in the list or directory will be
recursed and added. All files are stored relative to the given directory. */
/* @close Close the zip file. No further additions are possible. */
/* @setOption Sets a compression option. */
/******************************************************************************/
CLASS ZIP {
METHOD New( FILE file, STRING encoding, BOOLEAN append ) NATIVE "ZipMethods@builtins";
METHOD Empty() RETURNS( BOOLEAN empty ) NATIVE "ZipMethods@builtins";
METHOD Free() NATIVE "ZipMethods@builtins";
/* Methods */
METHOD addFile( FILE dir, FILE file, STRING comment ) RETURNS( BOOLEAN success ) NATIVE "ZipMethods@builtins";
METHOD addFiles( FILE dir, LIST list, BOOLEAN recurse ) RETURNS( BOOLEAN success )
{
if( !list ) {
list = dir.listFiles();
}
INTEGER i;
FILE f;
LIST sublist;
success = TRUE;
/* sort it to give a predictable order */
list.sort( comparator:this );
for( i = 0; i < list.length(); i++ ) {
f = list[i];
if( f.isDirectory() ) {
success = this.addFile( dir:dir, file:f ); // The addFile will skip this if we are not storing dirs.
if( !success ) {
return;
}
sublist = f.listFiles();
if( recurse && sublist ) {
success = this.addFiles( dir:dir, list:sublist, recurse:recurse );
if( !success ) {
return;
}
}
} else {
success = this.addFile( dir:dir, file:f );
if( !success ) {
return;
}
}
}
}
METHOD close( STRING comment ) RETURNS( BOOLEAN success ) NATIVE "ZipMethods@builtins";
STRING NODIRS = "nodirs";
STRING LEVEL = "level";
STRING NOTIME = "notime";
STRING STORE = "-1";
METHOD setOption( STRING option, STRING value ) NATIVE "ZipMethods@builtins";
private METHOD compare( FILE left, FILE rite ) RETURNS( INTEGER result )
{
STRING l = left.getPath();
STRING r = rite.getPath();
if( l < r ) {
result = -1;
} else if( l == r ) {
result = 0;
} else {
result = 1;
}
}
}
/******************************************************************************/
/*
** This class provides access to zip file entry.
*/
/* @isDirectory Is the item in the archive a directory. */
/* @isFile Is the item in the archive a file. */
/* @lastModified Gets the last modification time of the given file in the archive. */
/* @getSize Gets the size of the given file in the archive. */
/* @getCompressedSize Gets the compressed size of the given file in the archive. */
/* @getPath Gets the STRING path of the given file in the archive. */
/* @getFile Gets the FILE object of the given file in the archive. */
/* @getComment Gets the comment of the given file in the archive. */
/* @getCRC Gets the CRC value of the given file in the archive. */
/* @getMethod Gets the method used to store/compress the given file in the archive. */
/******************************************************************************/
CLASS ZIPENTRY {
METHOD Empty() RETURNS( BOOLEAN empty ) NATIVE "ZipEntryMethods@builtins";
INTEGER STORE = -1;
INTEGER DEFLATE = 8;
/* Methods */
METHOD isDirectory() RETURNS( BOOLEAN isdir ) NATIVE "ZipEntryMethods@builtins";
METHOD isFile() RETURNS( BOOLEAN isfile ) NATIVE "ZipEntryMethods@builtins";
METHOD lastModified() RETURNS( LONG time ) NATIVE "ZipEntryMethods@builtins";
METHOD getSize() RETURNS( LONG size ) NATIVE "ZipEntryMethods@builtins";
METHOD getCompressedSize() RETURNS( LONG size ) NATIVE "ZipEntryMethods@builtins";
METHOD getMethod() RETURNS( INTEGER method ) NATIVE "ZipEntryMethods@builtins";
METHOD getCRC() RETURNS( INTEGER crc ) NATIVE "ZipEntryMethods@builtins";
METHOD getPath() RETURNS( STRING path ) NATIVE "ZipEntryMethods@builtins";
METHOD getFile() RETURNS( FILE path ) NATIVE "ZipEntryMethods@builtins";
METHOD getComment() RETURNS( STRING comment ) NATIVE "ZipEntryMethods@builtins";
}
/******************************************************************************/
/*
** This class provides access to reading zip files.
*/
/* @New
// DESCRIPTION
Creates a new instance of UNZIP for reading a zip archive.
// ARGUMENTS
FILE file The input zip file.
STRING encoding The encoding of the file names. The default is UTF-8.
// EXAMPLE's
#load "sys/zip.elf";
FILE zipfile = new (FILE, path: "output.zip");
ZIP zip = new (UNZIP, file:zipfile);
FILE f = FILE.createTempDirectory();
zip.extractFiles( dir:f );
zip.close();
// NOTE
A zip archive can be read in two ways, one involves seeking in the archive and
the other does not. If one wishes to read an archive in order with no seeking
then each entry can be read in order using the getNextEntry call. Each entry
must be read then extracted, or skipped, in order. At any time this mode of
access can be abandoned by either extracting a previous entry out of order or
by calling readCentralDirectory.
The other way is to call readCentralDirectory to load the entire table of
contents for the archive. After this any entry may be accessed and/or extracted
in any order.
Prior to using either of these methods there are no entries known to this
object, since none have been loaded. Therefore one or the other must be used.
*/
/* @readCentralDirectory Reads the entire table of contents of the archive. */
/* @getNextEntry Gets the next entry for a archive being read linearly. */
/* @getEntry Gets the entry for a given item in the archive. */
/* @getEntryCount Gets the count of items in the archive. */
/* @extractFiles Extracts all files into the given output directory. */
/* @extractFile Extracts a single file into the given output directory with the given name. */
/* @close Close the zip file. No further accesses are possible. */
/* @setOption Sets an option. */
/******************************************************************************/
CLASS UNZIP {
METHOD New( FILE file, STRING encoding ) NATIVE "UnZipMethods@builtins";
METHOD Empty() RETURNS( BOOLEAN empty ) NATIVE "UnZipMethods@builtins";
METHOD Free() NATIVE "UnZipMethods@builtins";
/* Methods */
METHOD readCentralDirectory() RETURNS( BOOLEAN success ) NATIVE "UnZipMethods@builtins";
METHOD getNextEntry() RETURNS( ZIPENTRY entry ) NATIVE "UnZipMethods@builtins";
METHOD getEntryCount() RETURNS( INTEGER count ) NATIVE "UnZipMethods@builtins";
METHOD getEntry( INTEGER index ) RETURNS( ZIPENTRY entry ) NATIVE "UnZipMethods@builtins";
METHOD extractFile( ZIPENTRY entry, FILE dir, FILE file ) RETURNS( BOOLEAN success ) NATIVE "UnZipMethods@builtins";
METHOD extractFiles( FILE dir ) RETURNS( BOOLEAN success )
{
INTEGER i, count;
ZIPENTRY entry;
this.readCentralDirectory();
count = this.getEntryCount();
for( i = 0; i < count; i++ ) {
entry = this.getEntry( index:i );
if( !entry ) {
success = FALSE;
return;
}
success = this.extractFile( entry:entry, dir:dir, file:entry.getFile() );
if( !success ) {
return;
}
}
}
METHOD close() RETURNS( BOOLEAN success ) NATIVE "UnZipMethods@builtins";
METHOD setOption( STRING option, STRING value ) NATIVE "UnZipMethods@builtins";
}